package cqrtplm.service.impl;

import cn.hutool.core.collection.CollUtil;
import cqrtplm.service.FlowCodeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class FlowCodeServiceImpl implements FlowCodeService {
    private static final List<String> flowSequence = new ArrayList<>();
    private static final List<String> colorSequence = new ArrayList<>();

    // 排除 I、O、Q
    private static final char[] ALLOWED_CHARS = "0123456789ABCDEFGHJKLMNPRTUVWXYZ".toCharArray();

    static {
        buildFlowSequence();
        buildColorFlowSequence();
    }

    private static void  buildColorFlowSequence() {
        String digits = "0123456789";
        String letters = "ABCDEFGHJKLMNPRSTUVWXYZ";
        // 1. 数字+数字 (10-99)
        for (int i = 10; i <= 99; i++) {
            colorSequence.add(String.format("%02d", i));
        }
        // 2. 数字+字母 (0A-9Z)
        for (char d : digits.toCharArray()) {
            for (char l : letters.toCharArray()) {
                colorSequence.add("" + d + l);
            }
        }
    }

    private static void buildFlowSequence() {
        // 00 - 99
        for (int i = 0; i <= 99; i++) {
            flowSequence.add(String.format("%02d", i));
        }

        // 0A - 0Z
        for (char c : ALLOWED_CHARS) {
            if (c >= 'A' && c <= 'Z') {
                flowSequence.add("0" + c);
            }
        }

        // 1A - 9Z
        for (int d = 1; d <= 9; d++) {
            for (char c : ALLOWED_CHARS) {
                if (c >= 'A' && c <= 'Z') {
                    flowSequence.add(d + "" + c);
                }
            }
        }

        // A0 - A9
        for (char c : ALLOWED_CHARS) {
            if (c >= 'A' && c <= 'Z') {
                for (int d = 0; d <= 9; d++) {
                    flowSequence.add(c + "" + d);
                }
            }
        }

        // B0 - B9
        for (int d = 0; d <= 9; d++) {
            flowSequence.add("B" + d);
        }
    }

    @Override
    public String getNextFlowCode(String currentCode) {
        return getNextCode(currentCode, flowSequence);
    }

    @Override
    public int indexOfFlowCode(String currentCode) {
        int index = flowSequence.indexOf(currentCode);
        if (index == -1) {
            throw new IllegalArgumentException("无效的流水码: " + currentCode);
        }
        if (index + 1 >= flowSequence.size()) {
            throw new IllegalStateException("已到达最大流水码");
        }
        return index;
    }



    @Override
    public String getNextColorCode(String currentCode) {
        if(CollUtil.isEmpty(colorSequence)){
            buildColorFlowSequence();
        }
        return getNextCode(currentCode, colorSequence);
    }

    private String getNextCode(String currentCode, List<String> colorSequence) {
        int index = colorSequence.indexOf(currentCode);
        if (index == -1) {
            throw new IllegalArgumentException("无效的流水码: " + currentCode);
        }
        if (index + 1 >= colorSequence.size()) {
            throw new IllegalStateException("已到达最大流水码");
        }
        return colorSequence.get(index + 1);
    }
}
